package com.ruoyi.project.jiaowu.plan.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.jiaowu.plan.domain.*;
import com.ruoyi.project.jiaowu.plan.mapper.TbPlanMainMapper;
import com.ruoyi.project.jiaowu.plan.service.ITbPlanMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2019-10-23
 */
@Service
public class TbPlanMainServiceImpl implements ITbPlanMainService
{
    @Autowired
    private TbPlanMainMapper tbPlanMainMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public TbPlanMain selectTbPlanMainById(Long id)
    {
        return tbPlanMainMapper.selectTbPlanMainById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param tbPlanMain 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TbPlanMain> selectTbPlanMainList(TbPlanMain tbPlanMain)
    {
        return tbPlanMainMapper.selectTbPlanMainList(tbPlanMain);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param tbPlanMain 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTbPlanMain(TbPlanMain tbPlanMain)
    {
        tbPlanMain.setCreateTime(DateUtils.getNowDate());
        return tbPlanMainMapper.insertTbPlanMain(tbPlanMain);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param tbPlanMain 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTbPlanMain(TbPlanMain tbPlanMain)
    {
        tbPlanMain.setUpdateTime(DateUtils.getNowDate());
        return tbPlanMainMapper.updateTbPlanMain(tbPlanMain);
    }

    /**
     * 删除【请填写功能名称】对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTbPlanMainByIds(String ids)
    {
        tbPlanMainMapper.deletePlanCourse(Convert.toStrArray(ids));
        int a=tbPlanMainMapper.deleteTbPlanMainByIds(Convert.toStrArray(ids));
        return a;
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteTbPlanMainById(Long id)
    {
        return tbPlanMainMapper.deleteTbPlanMainById(id);
    }

    @Override
    public List<TbPlabAddListDto> selectAddPlanList(Long majorId) {
        return tbPlanMainMapper.selectAddPlanList(majorId);
    }

    /**
     * 添加培养计划
     * @param tbPlanMain
     * @return
     */
    @Override
    @Transactional
    public AjaxResult insertTbPlanMainList(List<AddPlanListParam> tbPlanMain) {
        PlanParam pp = new PlanParam(tbPlanMain.get(tbPlanMain.size()-1).getPlanName(),tbPlanMain.get(tbPlanMain.size()-1).getMajorId(),tbPlanMain.get(tbPlanMain.size()-1).getVersion(),tbPlanMain.get(tbPlanMain.size()-1).getBeginEffectiveTime());
        Long id=tbPlanMainMapper.insertPlan(pp);
        Long id2=pp.getId();
        Iterator<AddPlanListParam> iter = tbPlanMain.iterator();
        while(iter.hasNext()){
            AddPlanListParam center = iter.next();
            if(center.getCourseId()==null || center.getCourseId().equals("")){
                iter.remove();
            }
            center.setPlanId(id2);
        }
        System.out.println(tbPlanMain);
        tbPlanMainMapper.insertTbPlanMainList(tbPlanMain);
        return new AjaxResult(AjaxResult.Type.SUCCESS,"保存成功！");
    }



    @Override
    public List<EditTbPlanMain> selectEditPlanList(Long majorId) {
        return tbPlanMainMapper.selectEditPlanList(majorId);
    }

    @Override
    public List<String> getAllCourseId(int planId) {
        return tbPlanMainMapper.getAllCourseId(planId);
    }


    @Override
    @Transactional
    public boolean deletePlan(Long planId) {
        //删除plan表中的计划
        tbPlanMainMapper.deletePlan(planId);
        int a= tbPlanMainMapper.deleteCourse(planId);
        //System.err.println("dsacvfkjdhnkjv"+a);
        return true;
    }

    @Override
    public EditTbPlanMain selectEditplan(Map<String,Object> map) {
        return tbPlanMainMapper.selectEditplan(map);
    }

    /**
     * 发布培养计划
     * @param id
     * @return
     */
    @Override
    public boolean fabu(Integer id) {
        return tbPlanMainMapper.fabu(id);
    }

    /**
     * 取消发布培养计划
     * @param id
     * @return
     */
    @Override
    public boolean quxiaoFabu(Integer id) {
        return tbPlanMainMapper.quxiaoFabu(id);
    }

}












